package com.houtai.controller.train;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.train.TdTrainDetailDto;
import com.houtai.bean.dto.train.TdTrainListDto;
import com.houtai.bean.dto.train.apply.TdTrainApplyInsertDto;
import com.houtai.bean.dto.train.apply.TdTrainApplySendMessageDto;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.train.TdTrainDetailVo;
import com.houtai.bean.vo.train.TdTrainListVo;
import com.houtai.service.dict.DictService;
import com.houtai.service.train.TrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="培训接口", tags={"培训接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class TrainController {
    @Autowired
    private DictService dictService;

    @Autowired
    private TrainService trainService;

    @ApiOperation(value="培训列表", notes="培训列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/train/list")
    public TdTrainListVo list (@ApiParam(value = "培训列表所需参数") @Valid @RequestBody TdTrainListDto dto, BindingResult result) {
        TdTrainListVo retInfo = new TdTrainListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = trainService.trainList(dto);
        }
        return retInfo;
    }

    @ApiOperation(value="培训详情", notes="培训详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/train/detail")
    public TdTrainDetailVo detail (@ApiParam(value = "培训详情所需参数") @Valid @RequestBody TdTrainDetailDto dto, BindingResult result) {
        TdTrainDetailVo retInfo = new TdTrainDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = trainService.detail(dto);
        }
        return retInfo;
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="栏目分类列表", notes="栏目分类列表")
    @RequestMapping(value = "/train/cate/list", method = RequestMethod.POST)
    public DictListResult cate() {
        return dictService.findDictByType("train_cate");
    }
    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="上课制度列表", notes="上课制度列表")
    @RequestMapping(value = "/train/way/list", method = RequestMethod.POST)
    public DictListResult way() {
        return dictService.findDictByType("class_way");
    }
    @ApiOperation(value="发送验证码", notes="发送验证码")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/train/apply/send/message")
    public ResultVo sendMessage (@ApiParam(value = "发送验证码所需参数") @Valid @RequestBody TdTrainApplySendMessageDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = trainService.sendMessage(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="报名培训", notes="报名培训")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/train/apply")
    public ResultVo apply (@ApiParam(value = "报名培训所需参数") @Valid @RequestBody TdTrainApplyInsertDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = trainService.trainApply(dto);
        }
        return retInfo;
    }
}
